<?php

	/*
		Jax Calendar v1.34 - Copyright (c) 2002-2004, Andreas John aka Jack (tR)
		This program and it's moduls are Open Source in terms of General Public Licens (GPL) v2.0

		calendar.inc.php 		(the calendar control)
	*/


	// fit if nescessary:
	#define( "PATH_TO_SCRIPT", "../" );
	
	// Global variables (have to be fit)	
	require_once ( dirname(__FILE__) . "/"."main.inc.php" );

	if (empty($calendar_open_url)) $calendar_open_url = $PHP_SELF;
	
	$booked_events = array(); 
	for ( $i=1;$i<=31;$i++ ) $booked_events[$i] = 0;	

	// marks all days in calendar which are start day of an event
	if ($cal_show_reserved)
	{
		$red_data = array();
		$calendar_list->get_entry(0,$red_data);
		
		do {
			if ( (gmdate( "m", $red_data["event_start"] ) == $cal_month) && 
			     (gmdate( "Y", $red_data["event_start"] ) == $cal_year))
			{ 
				$xday = gmdate( "j", $red_data["event_start"] );
				$booked_events[ $xday ]++;
			}
	
			$calendar_list->get_next_entry( $red_data );
		} while (!$calendar_list->eol());	
	}

	$nxy= $cal_year+1;
	$pvy= $cal_year-1;

	if ($cal_today_selector) 
	{
		$ts = <<<HTML
	<tr> 
	  	<td colspan="3" class="today_select" id="today_select"><a href="$PHP_SELF?Y=$now_year&m=$now_month&d=$now_day&$ox_tail&view=$view"><img border="0" src="{$base_url["jax_cal"]}/images/back_today.gif">{$loc_lang["today"]}</a></td>
	</tr>
HTML;
	}
	else 
		$ts = "";

if ($use_quick_selectors)
{
	$mon_links = "<table>";

	for ($i=1;$i<=12;$i++)
	{
		$mon_links .= "<tr><td><a href=\"$PHP_SELF?Y=$cal_year&m=$i&d=$cal_day&$ox_tail\">{$month_name[$i]}</a></td></tr>";
	}
	$mon_links .= "</table>";

	$prev_years_links = $next_years_links = "<table>";
	for ($i=1;$i<=10;$i++)
	{
		$nxt = $cal_year + $i;
		$prv = $cal_year - $i;
		
		$prev_years_links .= "<tr><td><a href=\"$PHP_SELF?Y=$prv&m=$cal_month&d=$cal_day&$ox_tail\">$prv</a></td></tr>";
		$next_years_links .= "<tr><td><a href=\"$PHP_SELF?Y=$nxt&m=$cal_month&d=$cal_day&$ox_tail\">$nxt</a></td></tr>";
	}
	$prev_years_links .= "</table>";
	$next_years_links .= "</table>";
	
	$left_qs = <<<LINK
onMouseDown="setTimeout('qs_pop_up(\'qs_months\',\'left_qs\',-10, 0);',500);"
LINK;

    $right_qs = <<<LINK
onMouseDown="setTimeout('qs_pop_up(\'qs_months\',\'right_qs\',-10, 0);',500);"
LINK;

    $prev_years_qs = <<<LINK
onMouseDown="setTimeout('qs_pop_up(\'qs_prev_years\',\'prev_years_qs\',-10, 0);',500);"
LINK;

    $next_years_qs = <<<LINK
onMouseDown="setTimeout('qs_pop_up(\'qs_next_years\',\'next_years_qs\',-20, 0);',500);"
LINK;

echo 
<<< SCRIPTZ
<div id="qs_months" class="qs_months">
	$mon_links
</div>

<div id="qs_prev_years" class="qs_prev_years">
	$prev_years_links
</div>

<div id="qs_next_years" class="qs_next_years">
	$next_years_links
</div>

<script language="JavaScript" type="Text/JavaScript" src="{$base_url["jax_cal"]}/modules/quick_select_popup.js"></script>
SCRIPTZ;

}

	echo <<<CALENDAR_HEAD


<!-- THE CALENDAR CONTROL (START) -->
<div class="calendar">
	<table width="100%" border="0" cellspacing="0" cellpadding="0" class="headline">
	<tr> 
				<td align="center"><a id="prev_years_qs" $prev_years_qs href="$PHP_SELF?Y=$pvy&m=$cal_month&d=$cal_day&$ox_tail&view=$view"><img border="0" src="{$base_url["jax_cal"]}/images/prev_year_wb.gif" title="{$loc_lang["view_quick_select"]}"></a><a id="left_qs" $left_qs href="$PHP_SELF?Y=$cal_prev_year&m=$cal_prev_month&d=$cal_day&$ox_tail&view=$view"><img border="0" src="{$base_url["jax_cal"]}/images/prev_month_wb.gif" title="{$loc_lang["view_quick_select"]}"></a></td>
                <td align="center">$month_name[$cal_month] - $cal_year</td>
                <td align="center"><a id="right_qs" $right_qs href="$PHP_SELF?Y=$cal_next_year&m=$cal_next_month&d=$cal_day&$ox_tail&view=$view"><img border="0" src="{$base_url["jax_cal"]}/images/next_month_wb.gif" title="{$loc_lang["view_quick_select"]}"><a id="next_years_qs" $next_years_qs href="$PHP_SELF?Y=$nxy&m=$cal_month&d=$cal_day&$ox_tail&view=$view"><img border="0" src="{$base_url["jax_cal"]}/images/next_year_wb.gif" title="{$loc_lang["view_quick_select"]}"></a></td>
	</tr>
	$ts
	</table>    

	<table width="100%" border="0" class="overview">
	<tr> 
		<td class="dayname">{$loc_lang["mo"]}</td>
		<td class="dayname">{$loc_lang["tu"]}</td>
		<td class="dayname">{$loc_lang["we"]}</td>
		<td class="dayname">{$loc_lang["th"]}</td>
		<td class="dayname">{$loc_lang["fr"]}</td>
		<td class="dayname">{$loc_lang["sa"]}</td>
		<td class="dayname">{$loc_lang["su"]}</td>
	</tr>

CALENDAR_HEAD;

	if ( ( $cal_year == $now_year ) && ( $cal_month == $now_month ) )
		$today_day = $now_day;
	else
		$today_day = 0;

	$days_last_month = gmdate( "d", gmmktime(0,0,0,$cal_month,0,$cal_year ) );
	$days_this_month = gmdate( "d", gmmktime(0,0,0,$cal_next_month, 0, $cal_next_year ) );

	$first_day_this_month = gmmktime( "0","0","0",$cal_month, "1", $cal_year );
	$l_tm = localtime( $first_day_this_month, 1);

	//	$posx = array();
	
	// what's the weekday of the 1st day of this month?*/
	$first_day_pos = $l_tm[ "tm_wday" ];
	
	
	if ( $first_day_pos == 0 ) $first_day_pos = 7; // convert to Mo=1 to Su=7

	$day_num = $days_last_month - ($first_day_pos-2); $class=' class="last_month"';	
  
	for ( $y=1; $y<=6; $y++ )
	{
		echo "	<tr>\n";
		for ( $x=1; $x<=7; $x++ )
		{
			if ( ($y==1) && ($x==$first_day_pos) ) 
			{ 
				$day_num = 1; $class="";
			}
			
			if ( ($y >1) && ($day_num==$days_this_month+1) ) 
			{ 
				$day_num = 1; $class=' class="next_month"'; 
			}
			
			if ( ($class=="") && ($day_num == $today_day) )	$id=' id="today"'; else $id="";
			if ( ( $id!=' id="today"') && ($class=="") && ($day_num == $cal_day ) ) $id=' id="selected"';

			if ( $class != "" ) 
				{ $ap1 = ""; $ap2 = ""; }
			else 
				{ $ap1 = '<a href="'.$calendar_open_url."?Y=$cal_year&m=$cal_month&d=$day_num&$ox_tail".'" target="'.$calendar_open_target.'">'; $ap2 = '</a>'; }
				
			if (($booked_events[$day_num] != 0) && $class=="") 
				echo '		<td'.$class.$id.'>'.$ap1.'<span class="reserved">'.$day_num.'</span>'.$ap2.'</td>'."\n";
			else
				echo '		<td'.$class.$id.'>'.$ap1.$day_num.$ap2.'</td>'."\n";
			
			$day_num++;								
		}
		echo "	</tr>\n";
	}  


echo <<<CALENDAR_TAIL
  </table>
</div>

<!-- THE CALENDAR CONTROL (END) -->
CALENDAR_TAIL;

?>